/**
  * @description: 订单详情页面
  * @author: yingying.fan
  * @date: 2019/3/7 11:33
  */
import React, { Component } from 'react'
import { StyleSheet, SafeAreaView, ScrollView, DeviceEventEmitter } from 'react-native'
import { px2dp, LogUtils } from 'rn-xiaobu-utils'
import { getOrderDetailForCommon } from '../api/OrderApi'
import DetailOrderItem from '../components/orderdetail/DetailOrderItem'
import { OrderOptionEvent, OrderOptionTypes } from '../utils/OrderOptionUtils'
import NavigationService from '../../src/utils/NavigationService'
import AddrInfo from '../components/orderdetail/AddrInfo'
import PayInfo from '../components/orderdetail/PayInfo'
import TimeInfo from '../components/orderdetail/TimeInfo'
import OrderState from '../components/orderdetail/OrderState'

export default class OrderDetailPage extends Component {
  static navigationOptions = {
    headerTitle: '订单详情'
  }

  constructor (props) {
    super(props)
    this.state = {
      orderInfo: null
    }

    // this.orderId = this.props.navigation.getParam('orderId', '')
    this.orderId = '2017091119440896730'
    this._getOrderDetal()
  }

  componentDidMount (): void {
    this.orderDetailListener = DeviceEventEmitter.addListener(
      OrderOptionEvent,
      eventInfo => {
        LogUtils.log('接收事件:orderOption', eventInfo)
        let type = eventInfo.type
        // let orderIndx = eventInfo.info.INDEX
        switch (type) {
          case OrderOptionTypes.DELETE:
            NavigationService.back()
            break
          case OrderOptionTypes.CLOSE:
            NavigationService.back()
            break
        }
      })
  }

  componentWillUnmount (): void {
    if (this.orderDetailListener) {
      this.orderDetailListener.remove()
    }
  }

  _getOrderDetal () {
    getOrderDetailForCommon(this.orderId).then(body => {
      this.setState({
        orderInfo: body
      })
    }).catch(() => {

    })
  }

  render () {
    let page = null
    if (this.state.orderInfo) {
      page = []
      page.push(<OrderState key="order_detail_state" orderInfo={this.state.orderInfo}/>)
      page.push(<DetailOrderItem key="order_detail_item" orderInfo={this.state.orderInfo}/>)
      if (this.state.orderInfo.RECEIVER) {
        page.push(<AddrInfo key="order_detail_addr" orderInfo={this.state.orderInfo}/>)
      }
      page.push(<PayInfo key="order_detail_pay" orderInfo={this.state.orderInfo}/>)
      page.push(<TimeInfo key="order_detail_time" orderInfo={this.state.orderInfo}/>)
    }
    return (
      <SafeAreaView style={styles.container}>
        <ScrollView>
          {page}
        </ScrollView>
      </SafeAreaView>
    )
  }
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    backgroundColor: '#f2f5f6'
  },
  wrapper: {
    width: px2dp(690)
  }

})